package common.com.androidbase.fresco;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.common.sdk.net.connect.http.ImageRequestManager;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import common.com.androidbase.R;

/**
 * Created by sangchengjiang on 16/8/2.
 */
public class TwoColumnImageAdater extends BaseAdapter {

    private Context mContext;
    private ListView mListView;
    private List<TwoImages> mImages;

    private LayoutInflater mLayoutInflater;

    public TwoColumnImageAdater(Context context, ListView listView, List<TwoImages> images) {
        mContext = context;
        mListView = listView;
        mImages = images;

        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mImages == null ? 0 : mImages.size();
    }

    @Override
    public Object getItem(int i) {
        return mImages == null ? null : mImages.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = mLayoutInflater.inflate(R.layout.vw_item_two_img, null);
            holder.imageLeft = (SimpleDraweeView) view.findViewById(R.id.image_left);
            holder.imageRight = (SimpleDraweeView) view.findViewById(R.id.image_right);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        TwoImages twoImages = (TwoImages)getItem(i);
        ImageRequestManager.getInstance().startImageRequest(holder.imageLeft, twoImages.getLeftUrl());
        ImageRequestManager.getInstance().startImageRequest(holder.imageRight, twoImages.getRightUrl());

        return view;
    }

    public class ViewHolder {
        public SimpleDraweeView imageLeft;
        public SimpleDraweeView imageRight;
    }
}
